import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AdminModule } from './admin/admin.module';
import { ApiModule } from './api/api.module';
import { SharedModule } from './shared/shared.module';
// import { LoggerModule } from './logger/logger.module';
// import { WinstonModule } from 'nest-winston';
// import * as winston from 'winston';
// import * as chalk from 'chalk';
import {
  AcceptLanguageResolver,
  CookieResolver,
  HeaderResolver,
  I18nModule,
  QueryResolver,
} from 'nestjs-i18n';
import * as path from 'node:path';
import methodOverride from 'src/shared/middlewares/method-override';
// 定义日志级别颜色
// const levelsColors = {
//   error: 'red',
//   warn: 'yellow',
//   info: 'green',
//   debug: 'blue',
//   verbose: 'cyan',
// };

@Module({
  imports: [
    // WinstonModule.forRoot({
    //   transports: [
    //     new winston.transports.Console({
    //       format: winston.format.combine(
    //         winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    //         winston.format.printf(({ level, message, timestamp }) => {
    //           const chalkColor = chalk[levelsColors[level]];
    //           return `[Nest] ${process.pid}  - ${timestamp}     ${chalkColor(level.toUpperCase())} ${message}`;
    //         }),
    //       ),
    //     }),
    //     new winston.transports.File({
    //       filename: 'logs/error.log',
    //       level: 'error',
    //     }),
    //   ],
    // }),
    // LoggerModule,
    // 配置多语言模块
    I18nModule.forRoot({
      fallbackLanguage: 'en', // 默认语言
      loaderOptions: {
        path: path.join(__dirname, '/i18n/'),
        watch: true,
      },
      resolvers: [
        // url传递查询字符串/app?lang=zh /app?l=zh
        {
          use: QueryResolver,
          options: ['lang', 'l'],
        },
        // header传递: headers: { 'accept-language': 'zh-CN,zh;q=0.9' }
        AcceptLanguageResolver,
        // cookie传递: headers: { 'x-custom-lang': 'lang=zh-CN' }
        {
          use: HeaderResolver,
          options: ['x-custom-lang'],
        },
        // cookie: x-custom-lang=zh
        new CookieResolver(),
      ],
    }),
    SharedModule,
    AdminModule,
    ApiModule,
  ],
  controllers: [],
  providers: [],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(methodOverride).forRoutes('*');
  }
}
